package com.github.phoenics.teacher.itheima.netty.future;

import java.util.concurrent.*;

/**
 * @author Phoenics Chow
 * on 2020年04月14日
 */
public class AddTask implements Callable<Integer> {

    private int a,b;

    public AddTask(int a, int b) {
        this.a = a;
        this.b = b;
    }

    @Override
    public Integer call() throws Exception {
        Integer result = a + b;
        return result;
    }

    public static void main(String[] args) throws InterruptedException, ExecutionException {
        ExecutorService executor = Executors.newSingleThreadExecutor();
        //JDK目前为止返回的都是FutureTask的实例
        Future<Integer> future = executor.submit(new AddTask(1, 2));
        while (!future.isDone()){
            //该干啥干啥--------------
            //如果没有可干的,break也行
            //这个做法不好,应该是线程执行完成后,通知最好
        }
        // 只有当future的状态是已完成时(future.isDone() = true),get()方法才会返回
        //没有就等
        Integer result = future.get();
    }
}
